<?php

class m_form_documents_Record extends m_form_documents_RecordBase
{

	private static $instances;
	
	/**
	 * @return m_form_documents_Record
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_form_documents_Record
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		
		if(!isset(self::$instances[$lang][$id]))
		{
			$cache = f_core_Cache::getInstance();
			
			$key = $cache->getDocumentCacheKey($id, $lang);
			
			if($cache->hasCache($key))
			{
				return $cache->getCacheContent($key);
			}
			
			self::$instances[$lang][$id] = new self($id, $lang);
			
			$cache->putInCache($key, self::$instances[$lang][$id]);
			
			return self::$instances[$lang][$id];			
		}

		return self::$instances[$lang][$id];
	}
	
	public function postSave()
    {
        if(count($this->getFormValues()) > 0 && count($this->formValues) == 0)
        {
            return true;
        }
        
        if(count($this->formValues) > 0)
        {
            $dbProvider = f_database_Provider::getInstance();

            foreach($this->formValues as $name => $formValue)
            {
                $values = array('structureId' => $this->getStructureId(), 'recordId' => $this->getUniqueId(), 'name' => $name, 'value' => $formValue);
                $dbProvider->insert('m_form__field', $values);
            }
        }
        else
        {
            return false;
        }
    }

	public function __call($method, $attr)
	{
		$prefix = substr($method, 0, 3);
		$suffix = substr($method, 3);

        if($prefix == "get")
		{
            if(count($this->formValues) == 0)
            {
                $this->formValues = $this->getFormValues();
            }

            if(isset($this->formValues[$suffix]))
            {
                return $this->formValues[$suffix];
            }
   		}
        return null;
	}

    public function getFormValue($field)
    {
        if(count($this->formValues) == 0)
        {
            $this->formValues = $this->getFormValues();
        }
        if(isset($this->formValues[$field]))
        {
            return $this->formValues[$field];
        }
        return null;
    }

    private $formValues = array();

    /**
     *
     * @param array $formValues
     * @return m_form_documents_Record
     */
    public function setFormValues($formValues)
    {
        if(is_array($formValues) && count($formValues) > 0)
        {
            $this->formValues = $formValues;
        }
        return $this;
    }

    public function getFormValues()
    {
        $dbProvider = f_database_Provider::getInstance();

        $sql = "SELECT name,value FROM `m_form__field` WHERE `recordId`=:recordId";

        return $dbProvider->setQuery($sql)->execute(array('recordId' => $this->getUniqueId()))->getQueryResults(PDO::FETCH_KEY_PAIR);
    }
}